package com.example.practice.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.activity.*;

import com.example.practice.R;
import com.example.practice.utils.SharedpreferencesUtils;

public class MyInfoViewYl {
    public ImageView iv_head_icon_yl;
    private LinearLayout ll_head_yl;
    private RelativeLayout rl_course_history_yl,rl_setting_yl;
    private TextView tv_username_yl;
    private Activity context_yl;
    private LayoutInflater inflater_yl;
    private View currentView_yl;

    public MyInfoViewYl(Activity context){
        context_yl=context;
        inflater_yl=LayoutInflater.from(context);
    }

    /**
     * 设置我的界面用户名
     */
    public void setLoginParams(boolean isLogin){
        if(isLogin) {
            tv_username_yl.setText(SharedpreferencesUtils.readLoginUserName(context_yl));
        }
        else {
            tv_username_yl.setText("点击登录");
        }
    }

    /**
     * 初始化界面
     */
    private void initView(){
        currentView_yl=inflater_yl.inflate(R.layout.view_myinfo_yl,null);
        ll_head_yl =currentView_yl.findViewById(R.id.head_yl);
        iv_head_icon_yl=currentView_yl.findViewById(R.id.head_icon_yl);
        rl_course_history_yl=currentView_yl.findViewById(R.id.rl_course_history_yl);
        rl_setting_yl=currentView_yl.findViewById(R.id.rl_setting_yl);
        tv_username_yl=currentView_yl.findViewById(R.id.head_username_yl);
        currentView_yl.setVisibility(View.VISIBLE);
        setLoginParams(SharedpreferencesUtils.readLoginStatus(context_yl));
        ll_head_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedpreferencesUtils.readLoginStatus(context_yl)){
                    Intent intent=new Intent(context_yl,UserInfoActivity_yl.class);
                    context_yl.startActivity(intent);
                }
                else {
                    Intent intent=new Intent(context_yl,loginActivity_yl.class);
                    context_yl.startActivityForResult(intent,1);
                }
            }
        });
        rl_course_history_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedpreferencesUtils.readLoginStatus(context_yl)){
//                    跳转到播放记录
                    Intent intent =new Intent(context_yl, ylPlayHistoryActivity.class);
                    context_yl.startActivity(intent);
                }
                else {
                    Toast.makeText(context_yl,"您还未登录,请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });
        rl_setting_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(SharedpreferencesUtils.readLoginStatus(context_yl)){
                    //跳转到设置界面
                    Intent intent=new Intent(context_yl,settingActivity_yl.class);
                    context_yl.startActivityForResult(intent,1);
                }
                else {
                    Toast.makeText(context_yl,"你还未登录,请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void createView() {
        initView();
    }

    /**
     * 获取当前的view
     */
    public View getView() {
        if(currentView_yl==null){
            createView();
        }
        return currentView_yl;
    }

    /**
     * 显示当前view界面
     */
    public void showView(){
        if(currentView_yl==null){
            createView();
        }
        currentView_yl.setVisibility(View.VISIBLE);
    }
}


